﻿using PageAdmin.Utils.ORM.PropertyDescriptor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using PageAdmin.Utils.ORM;

namespace PageAdmin.Utils.BLL
{
    /// <summary>
    /// 内容页提交数据更新BLL
    /// </summary>
    public class EditBLL<T> : IEdit<T> where T : class, new()
    {
        /// <summary>
        /// 执行前的委托
        /// </summary>
        public Func<T, TipsInfo> Executing { get; set; }

        /// <summary>
        /// 执行后的委托
        /// </summary>
        public Func<T, TipsInfo> Executed { get; set; }

        /// <summary>
        /// 数据库对象
        /// </summary>
        private DataBaseContext _DbContext;

        public EditBLL(DataBaseContext dataBaseContext = null)
        {
            _DbContext = dataBaseContext;
        }

        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="tModel"></param>
        /// <returns></returns>
        public TipsInfo Edit(T tModel)
        {
            TipsInfo tipsInfo = new TipsInfo();
            try
            {
                DbSet<T> dbset = _DbContext.DbSet<T>();
                if (Executing != null)
                {
                    tipsInfo = Executing.Invoke(tModel);
                }
                if (tipsInfo.State == 1)
                {
                    int number=dbset.Update(tModel,GetLimitFields(dbset));
                    if (number == 0)
                    {
                        tipsInfo.Msg = "无效的id";
                        tipsInfo.State = 0;
                        return tipsInfo;
                    }
                }
                var proInfo = dbset.ModelType.GetProperty("Id", BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                if (proInfo == null)
                {
                    tipsInfo.State = 0;
                    tipsInfo.Msg = $"实体中的Id属性未设置";
                    return tipsInfo;
                }
                int detailId = (int)proInfo.GetValue(tModel, null);
                if (tipsInfo.State == 1)
                {
                    tipsInfo = updateAttachmentState(dbset, tModel, detailId);
                }
                if (tipsInfo.State == 1)
                {
                    tipsInfo = SubdataBLL.AddOrEditSubdata(_DbContext, tModel, detailId, "edit");
                }
                if (tipsInfo.State==1 && Executed != null)
                {
                    tipsInfo = Executed.Invoke(tModel);
                }
            }
            catch (Exception e)
            {
                tipsInfo.State = 0;
                tipsInfo.Msg = e.Message;
            }
            return tipsInfo;
        }

        /// <summary>
        /// 同步更新附件记录
        /// </summary>
        /// <returns></returns>
        private TipsInfo updateAttachmentState(DbSet<T> dbSet, T tModel,int id)
        {
            return AttachmentDataBLL.UpdateAttachmentState(dbSet, tModel, id);
        }

        /// <summary>
        /// 获取允许添加的字段
        /// </summary>
        /// <param name="dbSet"></param>
        /// <returns></returns>
        private IEnumerable<string> GetLimitFields(DbSet<T> dbSet)
        {
            IEnumerable<ModelFieldMapped> modelFieldMappedList = dbSet.ModelFieldMappingList.Where(c => c.AllowEdit == false);
            if (modelFieldMappedList.Count() <= 0)
            {
                return null;
            }
            return dbSet.ModelFieldMappingList.Where(c => c.AllowEdit == true).Select(c => c.FieldName);
        }

    }
}